package com.lingnan.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingnan.entity.po.JobSeeker;
import com.lingnan.entity.vo.Result;
import com.lingnan.service.JobSeekerService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user/seekers")
@Slf4j
public class JobSeekerController {
    
    @Autowired
    private JobSeekerService seekerService;
    
    /**
     * 创建求职者
     */
    @PostMapping
    public Result<JobSeeker> createSeeker(@RequestBody @Valid JobSeeker seeker) {
        JobSeeker created = seekerService.createSeeker(seeker);
        return Result.ok(created);
    }
    
    /**
     * 更新求职者
     */
    @PutMapping("/{seekId}")
    public Result<JobSeeker> updateSeeker(@PathVariable("seekId") Long seekId,
                                                 @RequestBody @Valid JobSeeker seeker) {
        JobSeeker updated = seekerService.updateSeeker(seekId, seeker);
        return Result.ok(updated);
    }
    
    /**
     * 删除求职者
     * 逻辑：删除前检查关联数据，如果存在关联数据则不允许删除
     * force: 是否强制删除，默认为false, 如果为true则强制删除
     * 强制删除会删除，对应简历，投递记录，面试记录
     */
@DeleteMapping("/{seekId}")
public Result<Map<String, Object>> deleteSeeker(@PathVariable("seekId") Long id,
                                                @RequestParam(value = "force",defaultValue = "false") boolean force) {
    Map<String, Object> result = new HashMap<>();
    try {
        boolean deleted = seekerService.deleteSeeker(id, force);
        if (deleted) {
            return Result.ok("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    } catch (RuntimeException e) {
        // 如果是因为有关联数据无法删除，返回检查结果
        Map<String, Object> conditions = seekerService.checkDeleteConditions(id);
        return Result.fail(conditions);
    } catch (Exception e) {
        // 处理其他可能的异常
        return Result.fail("删除过程中发生错误");
    }
}

    
    /**
     * 查询求职者详情
     */
    @GetMapping("/{id}")
    public Result<JobSeeker> getSeekerDetail(@PathVariable("id") Long id) {
        JobSeeker seeker = seekerService.getSeekerDetail(id);
        return Result.ok(seeker);
    }
    
    /**
     * 分页查询求职者
     * @param current 当前页码
     * size 每页大小
     * keyword 关键词,求职者真实名称或求职意向
     */
    @GetMapping
    public Result<IPage<JobSeeker>> pageSeekers(
            @RequestParam(value = "current",defaultValue = "1") int current,
            @RequestParam(value = "size",defaultValue = "10") int size,
            @RequestParam(value = "keyword",required = false) String keyword) {
        
        Page<JobSeeker> page = new Page<>(current, size);
        IPage<JobSeeker> result = seekerService.pageSeekers(page, keyword);
        return Result.ok(result);
    }
    
    /**
     * 检查删除条件
     */
    @GetMapping("/{id}/delete-check")
    public Result<Map<String, Object>> checkDeleteConditions(@PathVariable("id") Long id) {
        Map<String, Object> conditions = seekerService.checkDeleteConditions(id);
        return Result.ok(conditions);
    }
}